package com.ardf.controller;

import com.ardf.common.EntityConstants;
import com.ardf.entity.Algorithm;
import com.ardf.entity.dto.AlgorithmMenuDto;
import com.ardf.service.IAlgorithmService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/algorithm")
public class AlgorithmController {

    private final IAlgorithmService algorithmService;

    public AlgorithmController(IAlgorithmService algorithmService) {
        this.algorithmService = algorithmService;
    }

    /**
     * 请求算法数据
     */
    @GetMapping("/getAlgorithmMenu")
    public ResponseEntity<List<AlgorithmMenuDto>> getAlgorithmMenu(@RequestParam("ruleId") Long ruleId) {
        return ResponseEntity.ok(algorithmService.getAlgorithmMenu(ruleId));
    }

    /**
     * 设置最终结果
     */
    @PostMapping("/setLast")
    public void setLast(@RequestParam("algorithmId") Long algorithmId,
                        @RequestParam("isSet") Boolean isSet) {
        Algorithm algorithm = algorithmService.getById(algorithmId);
        if(isSet){
            if(EntityConstants.ALGORITHM_STATUS_NO_AGREED_NO_FINAL.equals(algorithm.getStatus())){
                algorithm.setStatus(EntityConstants.ALGORITHM_STATUS_NO_AGREED_FINAL);
            }else {
                algorithm.setStatus(EntityConstants.ALGORITHM_STATUS_AGREED_FINAL);
            }
        }else {
            if(EntityConstants.ALGORITHM_STATUS_NO_AGREED_FINAL.equals(algorithm.getStatus())){
                algorithm.setStatus(EntityConstants.ALGORITHM_STATUS_NO_AGREED_NO_FINAL);
            }else {
                algorithm.setStatus(EntityConstants.ALGORITHM_STATUS_AGREED_NO_FINAL);
            }
        }
        algorithmService.updateById(algorithm);
    }
}
